<template>
	<view>
		<view class="qrcode">
			<!-- 二维码打印内容 -->
			<image src="../../static/menu/ico_cc_smcx.png" mode=""></image>

			<text>{{ '物资名称' }}</text>


		</view>


		<!-- <canvas canvas-id="shareCanvas" style="width: 240px; height: 240px;"></canvas> -->

		<button type="default" @click="printEvent">打印</button>
	</view>
</template>

<script>
	// import drawQrcode from '@/common/print/weapp.qrcode.esm.js'
	// import util from '@/common/print/util.js'

	const plug = uni.requireNativePlugin('Html5app-Gprinter');

	export default {
		data() {
			return {
				isOpenBle: false, //是否已经打开蓝牙，默认为false
				devicesList: [], //设备列表
				serviceList: [], //服务列表
				deviceId: "", //选中的deviceId
				canvas_width: 240,
				canvas_height: 240,
			}
		},
		methods: {
			async printEvent() {
				let Qrcode_res = await this.get_Qrcode();

				console.log(Qrcode_res);
			},
			get_Qrcode() {
				let self = this;
				return new Promise((resolve, reject) => {
					const ctx = uni.createCanvasContext('shareCanvas');
					console.log(self.canvas_width);
					ctx.clearRect(0, 0, self.canvas_width, self.canvas_height);

					drawQrcode({
						canvasId: 'shareCanvas',
						text: String('xiaonibaba.com'),
						width: self.canvas_width,
						height: self.canvas_height,
						callback(e) {
							setTimeout(() => {
								console.log(self.canvas_width);
								// 获取图片数据
								uni.canvasGetImageData({
									canvasId: 'shareCanvas',
									x: 0,
									y: 0,
									width: self.canvas_width,
									height: self.canvas_height,
									success(res) {
										resolve(util.zip_image(res));
									}
								})
							}, 1000);
						}
					});
				});
			},
			
			printerLable(){
				const _this=this;
				plug.printer({"TSC":_this.Lable()},ret=>{
					
					_this.tip=JSON.stringify(ret);
				});
			},
			
			//  标签内容设置
			Lable(){
				
				//制作标签格式， 
				var data=[];  //定义一个数组
				var line={};  //每添加一个，代表一行字
				
				//设置标签纸大小
				line={};
				line.width=40; //mm
				line.height=30; //mm
				line.gap=3; //标签纸之间，间隙长度 mm
				line.page=1; //打印几份
				data.push(line); //每添加一个，代表一行字
			
				//添加图片
				line={};
				line.image="http://www.html5-app.com/gprinter.png";
				line.x=30;
				line.y=20;
				line.width=70;
				data.push(line);
				
				
					//添加文字
				line={};
				line.text="HTML5+混合APP开发社区";
				line.rotation=0;
				line.x=30;
				line.y=100;
				line.xscal=1;
				line.yscal=1;
				data.push(line); //每添加一个，代表一行字
				
				//添加条型码
				line={};
				line.barcode="2019563352660";
				line.x=30;
				line.y=160;
				line.type="CODE128";
				line.height=50;
				line.readable=true;
				line.rotation=0;
				data.push(line);
				
				 //加入二维码
				line={};
				line.qrcode="1234566";	
				line.x=190;
				line.y=20;
				line.width=3
				line.rotation=0;
				data.push(line);
				
				return data;
			}
			
			
		}
		
	}
</script>

<style lang="scss">
	.qrcode {
		width: 500rpx;
		height: 400rpx;
		border-radius: 40rpx;

	}
</style>
